package cn.jiyun.fenlei.product.service.impl;

//import cn.jiyun.fenlei.product.config.ConstantPropertiesUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.jiyun.fenlei.product.feign.ManageFeign;
import cn.jiyun.fenlei.product.mapper.ProductMapper;
import cn.jiyun.fenlei.product.service.ProductService;
import cn.jiyun.model.Base;
import cn.jiyun.model.Product;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jdk.jshell.execution.Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    RedisTemplate<String, String> redisTemplate;
    @Autowired
    private ManageFeign manageFeign;

    @Override
    public List<Product> findAll(Product product) {
        List<Product> productList = productMapper.findAll();
//        queryWrapper
        LambdaQueryWrapper<Product> queryWrapper1 = new LambdaQueryWrapper<>();
        if (product.getId() != null) {
            queryWrapper1.eq(Product::getPid, 0);
            queryWrapper1.eq(Product::getId, product.getId());
        }
        if (product.getStatus() != null) {
            queryWrapper1.eq(Product::getStatus, product.getStatus());
        }
        if (product.getStatus() == null && product.getName() == null) {
            queryWrapper1.eq(Product::getPid, 0);
        }
        if (product.getName() != null) {
            queryWrapper1.like(Product::getName, product.getName());
        }
        queryWrapper1.eq(Product::getDel, 0);
        queryWrapper1.orderByDesc(Product::getPaixu);
        List<Product> productList1 = productMapper.selectList(queryWrapper1);
        List<Product> productList2 = new ArrayList<>();
        if (product.getStatus() != null) {
            List<Product> collect = productList1.stream().filter(yi1 -> yi1.getPid().equals(0)).collect(Collectors.toList());
            productList1.stream().forEach(yi2 -> {
                Integer a = 0;
                for (Product product1 : collect) {
                    if (yi2.getPid().equals(product1.getId())) {
                        a = 1;
                    }
                }
                if (a == 0) {
                    productList2.add(yi2);
                }
            });
        }
        ;
        if (product.getName() != null) {
            List<Product> collect1 = productList.stream().filter(yi1 -> yi1.getPid().equals(0)).collect(Collectors.toList());
            productList1.stream().forEach(yi2 -> {
                for (Product product1 : collect1) {
                    if (yi2.getPid() == 0) {
                        productList2.add(yi2);
                    } else {
                        if (yi2.getPid().equals(product1.getId())) {
                            productList2.add(product1);
                        }
                    }
                }
            });
        }
        if (product.getStatus() != null || product.getName() != null) {
            productList2.stream().forEach(yi -> {
                if (product.getStatus() != null) {
                    List<Product> collect1 = productList.stream().filter(yi1 -> yi.getId().equals(yi1.getPid())).filter(yi1 -> yi1.getDel().equals(0)).collect(Collectors.toList());
                    List<Product> collect = collect1.stream().filter(yi22 -> yi22.getStatus().equals(product.getStatus())).collect(Collectors.toList());
                    if (product.getStatus() == 1) {
                        yi.setChildren(collect);
                    } else {
                        yi.setChildren(collect1);
                    }
                }
                if (product.getName() != null) {
                    List<Product> collect66 = new ArrayList<>();
                    for (Product product1 : productList1) {
                        if (product1.getPid().equals(yi.getId())) {
                            collect66.add(product1);
                        } else {
                            List<Product> collect = productList.stream().filter(yiii -> yi.getId().equals(yiii.getPid())).filter(yi1 -> yi1.getDel().equals(0)).collect(Collectors.toList());
                            yi.setChildren(collect);
                        }
                    }
                    yi.setChildren(collect66);
                }
            });
        } else {
            productList1.stream().forEach(yi -> {
                List<Product> collect1 = productList.stream().filter(yi1 -> yi.getId().equals(yi1.getPid())).filter(yi1 -> yi1.getDel().equals(0)).collect(Collectors.toList());
                yi.setChildren(collect1);
            });
        }
        if (product.getStatus() != null || product.getName() != null) {
            return productList2;
        } else {
            return productList1;
        }
    }

    @Override
    public void updatestatus(Integer id) {
        Product product = productMapper.selectById(id);
        Integer status1;
        Integer status = product.getStatus();
        if (status == 2) {
            status1 = 1;
        } else {
            status1 = 2;
        }
        if (product.getPid() == 0) {
            LambdaQueryWrapper<Product> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
            productLambdaQueryWrapper.eq(Product::getPid, product.getId());
            List<Product> productList = productMapper.selectList(productLambdaQueryWrapper);
            for (Product product1 : productList) {
                productMapper.updatestatus(product1.getId(), status1);
            }
        }
        productMapper.updatestatus(id, status1);
    }

    @Override
    public List<Product> yijifindAll() {
        LambdaQueryWrapper<Product> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productLambdaQueryWrapper.eq(Product::getPid, 0);
        List<Product> productList = productMapper.selectList(productLambdaQueryWrapper);
        return productList;
    }

    @Override
    public List<Product> category() {
        List<Product> userList = new ArrayList<>();
        String s = redisTemplate.boundValueOps("Product").get();
        if (StringUtils.isNotBlank(s)) {
            JSONArray objects = JSONUtil.parseArray(s);
            for (Object object : objects) {
                JSONObject objects1 = (JSONObject) object;
                Product product = new Product();
                BeanUtil.fillBeanWithMap(objects1, product, true);
                userList.add(product);
            }
            return userList;
        }
        LambdaQueryWrapper<Product> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productLambdaQueryWrapper.eq(Product::getPid, 0);
        List<Product> productList = productMapper.selectList(productLambdaQueryWrapper);
        List<Product> productList1 = productMapper.selectList(null);
        productList.stream().forEach(yi -> {
            List<Product> collect = productList1.stream().filter(yi1 -> yi1.getPid().equals(yi.getId())).collect(Collectors.toList());
            yi.setChildren(collect);
        });
        redisTemplate.boundValueOps("Product").set(JSONUtil.toJsonStr(productList));
        return productList;

    }

    @Override
    public Page<Product> products(Integer page, Integer limit, Integer cid, Integer sid) {
        Page<Product> productPage = new Page<>(page, limit);
        LambdaQueryWrapper<Product> productLambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (sid != 0) {
            productLambdaQueryWrapper.eq(Product::getId, sid);
        } else {
            Product product = productMapper.selectById(cid);
            LambdaQueryWrapper<Product> productLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            productLambdaQueryWrapper1.eq(Product::getPid, product.getId());
            Page<Product> productPage1 = productMapper.selectPage(productPage, productLambdaQueryWrapper1);
            return productPage1;

        }
        Page<Product> productPage1 = productMapper.selectPage(productPage, productLambdaQueryWrapper);
        return productPage1;
    }

    @Override
    public List<Product> findByid(Integer id) {
        String s = redisTemplate.boundValueOps("Product").get();
        if (StringUtils.isNotBlank(s)) {
            JSONArray objects = JSONUtil.parseArray(s);
            List<Product> productList = new ArrayList<>();
            for (Object object : objects) {
                JSONObject objects1 = (JSONObject) object;
                Product product = new Product();
                BeanUtil.fillBeanWithMap(objects1, product, true);
                productList.add(product);
            }
            List<Product> collect = productList.stream().filter(yi -> yi.getId().equals(id)).collect(Collectors.toList());
            List<Product> products = new ArrayList<>();
            for (Product product : collect) {
                List<Product> children = product.getChildren();
                for (Product child : children) {
                    products.add(child);
                }
            }
            if (products.size() > 0) {
                return products;
            }
        }
        LambdaQueryWrapper<Product> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productLambdaQueryWrapper.eq(Product::getPid, 0);
        List<Product> productList = productMapper.selectList(productLambdaQueryWrapper);
        List<Product> productList1 = productMapper.selectList(null);
        productList.stream().forEach(yi -> {
            List<Product> collect = productList1.stream().filter(yi1 -> yi1.getPid().equals(yi.getId())).collect(Collectors.toList());
            yi.setChildren(collect);
        });
        redisTemplate.boundValueOps("Product").set(JSONUtil.toJsonStr(productList));
        List<Product> products = new ArrayList<>();
        for (Product product : productList1) {
            if (product.getPid() == id) {
                products.add(product);
            }
        }
        return products;
    }

    @Override
    public List<Base> findByidBase(Integer id) {
        Product product = productMapper.selectById(id);
        List<Base> byidBase = new ArrayList<>();
        if (product.getPid() == 0) {
            LambdaQueryWrapper<Product> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
            productLambdaQueryWrapper.eq(Product::getPid, product.getId());
            List<Product> children = productMapper.selectList(productLambdaQueryWrapper);
            for (Product product1 : children) {
                List<Base> byidbase = manageFeign.findByidBase(product1.getId());
                for (Base base : byidbase) {
                    byidBase.add(base);
                }
            }

        }else{
             byidBase = manageFeign.findByidBase(id);
            return byidBase;
        }
        return byidBase;
    }

}

//    @Override
//    public String uploader(MultipartFile multipartFile) {
//
//        String endpoint = ConstantPropertiesUtil.END_POINT;
//        String accessKeyId = ConstantPropertiesUtil.KEY_ID;
//        String accessKeySecret = ConstantPropertiesUtil.KEY_SECRET;
//        // 填写Bucket名称
//        String bucketName = ConstantPropertiesUtil.BUCKET_NAME;
//        /** 创建OSSClient实例。*/
//        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//        try {
//
//
//            /** 获取上传文件输入流*/
//            InputStream inputStream = multipartFile.getInputStream();
//
//            /**获取文件名称*/
//            String filename = multipartFile.getOriginalFilename();
//
//            /**调用oss方法实现上传*/
//            //第一个参数 Bucket名称
//            //第二个参数 上传到oss文件路径或文件名称
//            //第三个参数 上传文件输入流
//            ossClient.putObject(bucketName, filename, inputStream);
//
//            /** 返回上传到阿里OSS的路径*/
//            String url = "https://".concat(bucketName).concat(".").concat(endpoint).concat("/").concat(filename);
//
//            return url;
//        }catch (Exception e){
//            e.printStackTrace();
//            return null;
//        }finally {
//            if (ossClient != null) {
//                ossClient.shutdown();
//            }
//        }
//    }
//}
